package com.skivingcloud.admin.sys.controller;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysMsgDef;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.service.ISysMsgDefService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 消息规则定义 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/sys/msgDef")
public class SysMsgDefController {
	
	private static Logger log = LoggerFactory.getLogger(SysMsgDefController.class);

	@Autowired
	private ISysMsgDefService sysMsgDefService;

	/**
	 * 分页查询消息规则列表
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages;
		try {
			pages = sysMsgDefService.queryPage(params);
			return R.ok().put("page", pages);
		} catch (Exception e) {
			log.error("查询失败", e);
			return R.ok().put("page", null);
		}
	}

	/**
	 * 根据id获取消息规则信息
	 *
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysMsgDef msgDef = sysMsgDefService.selectById(id);
		return R.ok().put("msgDef", msgDef);
	}

	/**
	 * 保存消息规则信息
	 * 
	 * @param msgDef
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "保存消息规则配置")
	@PostMapping("/save")
	public R save(@RequestBody SysMsgDef msgDef) {
		try {
			User user = (User) UserUtil.getUser();
			LocalDateTime date = LocalDateTime.now();
			msgDef.setCreater(user.getId());
			msgDef.setCreateTime(date);
			msgDef.setModUser(user.getId());
			msgDef.setModTime(date);
			sysMsgDefService.saveMe(msgDef);
			return R.ok();
		} catch (Exception e) {
			log.error("保存消息规则失败", e);
			return R.error("保存消息规则失败");
		}
	}

	/**
	 * 修改消息规则信息
	 * 
	 * @param msgDef
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "update", description = "修改消息规则配置")
	@PostMapping("/update")
	public R update(@RequestBody SysMsgDef msgDef) {
		try {
			User user = (User) UserUtil.getUser();
			LocalDateTime date = LocalDateTime.now();
			msgDef.setModUser(user.getId());
			msgDef.setModTime(date);
			sysMsgDefService.updateMe(msgDef);
			return R.ok();
		} catch (Exception e) {
			log.error("更新消息规则失败", e);
			return R.error("更新消息规则失败");
		}
	}

	/**
	 * 删除消息规则信息
	 * 
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "delete", description = "删除消息规则配置")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			sysMsgDefService.delete(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除消息规则失败", e);
			return R.error("删除消息规则失败");
		}
	}

	/**
	 * 页面信息
	 *
	 * @throws Exception
	 */
	@GetMapping("/pageInfo/{id}")
	public R pageInfo(@PathVariable("id") String id) {
		SysMsgDef msgDef = sysMsgDefService.pageInfo(id);
		return R.ok().put("msgDef", msgDef);
	}
}
